#include "StdAfx.h"
#include "MoveShapesCmd.h"
#include "OpDrawShape.h"

RegisterCommandClass<CMoveShapesCmd> RegisterCommandClass(ClassNameToString(CMoveShapesCmd));


CMoveShapesCmd::CMoveShapesCmd(void)
{
	m_pData = NULL;
}


CMoveShapesCmd::~CMoveShapesCmd(void)
{
	delete m_pData;
}

bool CMoveShapesCmd::Execute()
{
	if (!CMyBaseCommand::Execute())
		return false;

	if (m_pData == NULL)
	{
		if (!GetDataFromXML())
			return false;
	}

	MoveShapes(m_pData->m_lstNewData);

	CModelObjOption::GetIModelObjEventHandler()->SetDocModified(true);
	return true;
}

bool CMoveShapesCmd::Unexecute()
{
	if (!CMyBaseCommand::Unexecute())
		return false;

	if (!m_pData)
	{
		if (!GetDataFromXML())
			return false;
	}

	MoveShapes(m_pData->m_lstOldData);		

	CModelObjOption::GetIModelObjEventHandler()->SetDocModified(true);

	return true;
}

bool CMoveShapesCmd::SaveDataToXML()
{
	Begin_SaveDataToXML

	TiXmlElement itemOldInfo("Old_moveshapes_info");	
	SaveShapeData2XML(m_pData->m_lstOldData,itemOldInfo);

	TiXmlElement itemNewInfo("New_moveshapes_info");
	SaveShapeData2XML(m_pData->m_lstNewData,itemNewInfo);

	End_SaveDataToXML
	return true;
}

bool CMoveShapesCmd::GetDataFromXML()
{
	if (m_pData)
		return true;
	else
		m_pData = new MoveShapesCmdData;

	Begin_GetDataFromXML

	TiXmlElement *elemOldInfo = elemCmd->FirstChildElement("Old_moveshapes_info");
	if (elemOldInfo)
		GetShapeDataFromXML(elemCmd,m_pData->m_lstOldData);    

	TiXmlElement *elemNewInfo = elemCmd->FirstChildElement("New_moveshapes_info");
	if (elemNewInfo)
		GetShapeDataFromXML(elemCmd,m_pData->m_lstNewData); 	

	End_GetDataFromXML
	return true;
}

bool CMoveShapesCmd::DeleteData()
{
	delete m_pData;
	m_pData = NULL;
	return true;
}

bool CMoveShapesCmd::PrepareData()
{
	m_pData = new MoveShapesCmdData;

	End_PrepareData	

	return true;
}

void CMoveShapesCmd::SetOldData( vector<CDisplayObj* > vecSelObjs )
{
	SetFullShapeData(vecSelObjs,m_pData->m_lstOldData);
}

void CMoveShapesCmd::SetNewData( vector<CDisplayObj* > vecSelObjs )
{
	SetFullShapeData(vecSelObjs,m_pData->m_lstNewData);
}

void CMoveShapesCmd::SetFullShapeData( vector<CDisplayObj*> vecShape,list<FullShapeData> &lstData)
{
	for (vector<CDisplayObj*>::iterator it=vecShape.begin(); it!=vecShape.end(); it++)
	{
		if (!(*it)->IsShapeDisObj())
		{
			continue;
		}
		FullShapeData data;
		CShape* pShape=(CShape*)(*it)->GetEntityObj();		
		CShapeDisplayObj* pShapeDisObj = (CShapeDisplayObj*)(*it);

		data.m_Id=pShape->GetId();
		data.m_startPos=pShapeDisObj->getStartPos();
		data.m_endPos=pShapeDisObj->getEndPos();
		data.m_dblLineWidth=pShapeDisObj->GetLineWidth();	
		data.m_FillClrR=pShapeDisObj->GetFillColorR();
		data.m_FillClrG=pShapeDisObj->GetFillColorG();
		data.m_FillClrB=pShapeDisObj->GetFillColorB();
		data.m_ClrR=pShapeDisObj->GetColorR();
		data.m_ClrG=pShapeDisObj->GetColorG();
		data.m_ClrB=pShapeDisObj->GetColorB();
		data.m_szTextFont=pShapeDisObj->GetTextFont();
		data.m_TxtClrR=pShapeDisObj->GetTextColorR();
		data.m_TxtClrG=pShapeDisObj->GetTextColorG();
		data.m_TxtClrB=pShapeDisObj->GetTextColorB();

		if (pShape->getShapeStyle()==SHAPE_POLYGON)
		{
			data.m_pts=((CPolygonDisplayObj*)pShapeDisObj)->GetAllPoints();
		}
		if (pShape->getShapeStyle()==SHAPE_IMAGE)
		{
			data.m_strPathName=((CImageObj *)pShape)->GetPathName();
		}
		if (pShape->getShapeStyle()==SHAPE_WORD)
		{
			data.m_WordText=((CWordDisplayObj*)pShapeDisObj)->GetWordText();
		}
		data.m_nShapeStyle=(int)pShape->getShapeStyle();

		lstData.push_back(data);
	}
}

bool CMoveShapesCmd::SaveShapeData2XML(list<FullShapeData> &lstData,TiXmlElement &parentElt)
{
	TiXmlElement itemMoveShapesData("Move_shapes_data");

	for (list<FullShapeData>::iterator it=lstData.begin(); it!=lstData.end(); it++)
	{
		FullShapeData  data= *it;

		TiXmlElement itemShape("Full_Shape_Data");

		itemShape.SetAttribute("Id", data.m_Id);

		HPoint startPos = data.m_startPos;
		string strStartPositon;
		ConvertHpoint2String(startPos, strStartPositon);
		itemShape.SetAttribute("StartPositon", strStartPositon);

		HPoint endPos = data.m_endPos;
		string strEndPositon;
		ConvertHpoint2String(endPos, strEndPositon);
		itemShape.SetAttribute("EndPositon", strEndPositon);

		itemShape.SetAttribute("LineWidth", data.m_dblLineWidth);
		itemShape.SetAttribute("FillClrR", data.m_FillClrR);
		itemShape.SetAttribute("FillClrG", data.m_FillClrG);
		itemShape.SetAttribute("FillClrB", data.m_FillClrB);
		itemShape.SetAttribute("ClrR", data.m_ClrR);
		itemShape.SetAttribute("ClrG", data.m_ClrG);
		itemShape.SetAttribute("ClrB", data.m_ClrB);
		itemShape.SetAttribute("ShapeStyle", data.m_nShapeStyle);
		itemShape.SetAttribute("PathName", CStringToStdstring(data.m_strPathName));
		itemShape.SetAttribute("WoedText", CStringToStdstring(data.m_WordText));

		itemShape.SetAttribute("TextFont", CStringToStdstring(data.m_szTextFont));
		itemShape.SetAttribute("TextClrR", data.m_TxtClrR);
		itemShape.SetAttribute("TextClrG", data.m_TxtClrG);
		itemShape.SetAttribute("TextClrB", data.m_TxtClrB);

		vector<HPoint> vecPoint =data.m_pts;
		for (vector<HPoint>::iterator iter=vecPoint.begin(); iter!=vecPoint.end(); iter++)
		{
			HPoint point = *iter;
			TiXmlElement itemPoint("Point");
			string strPoint;
			ConvertHpoint2String(point, strPoint);
			itemPoint.SetAttribute("HPoint", strPoint);
			itemShape.InsertEndChild(itemPoint);
		}
		itemMoveShapesData.InsertEndChild(itemShape);
	}
	parentElt.InsertEndChild(itemMoveShapesData);
	return true;
}

bool CMoveShapesCmd::GetShapeDataFromXML( TiXmlElement *pParentEle,list<FullShapeData> &lstData )
{
	TiXmlElement* itemDeleteShapeData = pParentEle->FirstChildElement("Move_shapes_data");
	if (itemDeleteShapeData)
	{
		for (TiXmlElement* itemFulldata=itemDeleteShapeData->FirstChildElement("Full_Shape_Data");
			itemFulldata; itemFulldata=itemFulldata->NextSiblingElement("Full_Shape_Data"))
		{
			FullShapeData  data;
			itemFulldata->Attribute("Id", &data.m_Id);

			string strStartPositon = itemFulldata->Attribute("StartPositon");
			ConvertString2Hpoint(strStartPositon, data.m_startPos);

			string strEndPositon = itemFulldata->Attribute("EndPositon");
			ConvertString2Hpoint(strEndPositon, data.m_endPos);

			itemFulldata->Attribute("LineWidth",&data.m_dblLineWidth);
			itemFulldata->Attribute("FillClrR",&data.m_FillClrR);
			itemFulldata->Attribute("FillClrG",&data.m_FillClrG);
			itemFulldata->Attribute("FillClrB",&data.m_FillClrB);
			itemFulldata->Attribute("ClrR",&data.m_ClrR);
			itemFulldata->Attribute("ClrG",&data.m_ClrG);
			itemFulldata->Attribute("ClrB",&data.m_ClrB);
			itemFulldata->Attribute("ShapeStyle",&data.m_nShapeStyle);
			string sPathName = itemFulldata->Attribute("PathName");
			StdstringToCString(sPathName,data.m_strPathName);
			string sWordText=itemFulldata->Attribute("WoedText");
			StdstringToCString(sWordText,data.m_WordText);

			string sTextFont=itemFulldata->Attribute("TextFont");
			StdstringToCString(sTextFont,data.m_szTextFont);
			itemFulldata->Attribute("TextClrR", &data.m_TxtClrR);
			itemFulldata->Attribute("TextClrG", &data.m_TxtClrG);
			itemFulldata->Attribute("TextClrB", &data.m_TxtClrB);
			//pologon
			for (TiXmlElement* pointElem =itemFulldata->FirstChildElement("Point");
				pointElem; pointElem=pointElem->NextSiblingElement("Point") )
			{
				HPoint temp;
				string strPoint = pointElem->Attribute("HPoint");
				ConvertString2Hpoint(strPoint,temp);
				data.m_pts.push_back(temp);
			}
			lstData.push_back(data);
		}
	}
	return true;
}

void CMoveShapesCmd::MoveShapes( list<FullShapeData> lstData )
{
	if (lstData.empty())
	{
		return;
	}

	DWORD dwView;
	CModelObjOption::GetIModelObjEventHandler()->GetHoopsView(dwView);
	HBaseView* pHview = (HBaseView*)dwView;
	HModelLayerModel* pHmodel=dynamic_cast<HModelLayerModel*>(pHview->GetModel());
	CSystem *pBigDrawing = pHmodel->GetBigDrawing();
	CLayer* pActiveLayer = pBigDrawing->GetActiveLayer();
	if(!pActiveLayer)
		return;

	CShape *pShape=NULL;
	CShapeDisplayObj *pShapeDisObj=NULL;
	std::list<CShape*> lstShape=pActiveLayer->getShapeList();
	list<FullShapeData>::iterator iter_shapedata=lstData.begin();
	for (;iter_shapedata!=lstData.end();iter_shapedata++)
	{
		FullShapeData data=*iter_shapedata;
		int id=(*iter_shapedata).m_Id;
		for (std::list<CShape*>::iterator iter=lstShape.begin();iter!=lstShape.end();iter++)
		{
			CShape *temp=*iter;
			if (temp->GetId()==id)
			{
				pShape=temp;
				break;
			}
		}
		if (pShape)
		{
			pShapeDisObj=(CShapeDisplayObj *)pShape->GetDisplayObjByLayerID(pActiveLayer->GetId());
			if (!pShapeDisObj)
			{
				continue;
			}
			pShapeDisObj->setStartPos(data.m_startPos);
			pShapeDisObj->setEndPos(data.m_endPos);
			pShapeDisObj->SetLineWidth(data.m_dblLineWidth);
			pShapeDisObj->SetFillColor(data.m_FillClrR,data.m_FillClrG,data.m_FillClrB);
			pShapeDisObj->SetColor(data.m_ClrR,data.m_ClrG,data.m_ClrB);
			pShapeDisObj->SetTextFont(data.m_szTextFont);
			pShapeDisObj->SetTextColor(data.m_TxtClrR,data.m_TxtClrG,data.m_TxtClrB);
			if (pShape->getShapeStyle()==SHAPE_POLYGON)
			{
				((CPolygonDisplayObj*)pShapeDisObj)->ChangeAllPoints(data.m_pts);
			}
			if (pShape->getShapeStyle()==SHAPE_IMAGE)
			{
				((CImageObj *)pShape)->SetPathName(data.m_strPathName);
			}
			if (pShape->getShapeStyle()==SHAPE_WORD)
			{
				((CWordDisplayObj*)pShapeDisObj)->SetWordText(data.m_WordText);
			}
			COpDrawShape  *m_pDrawShape = new COpDrawShape(pHview);
			m_pDrawShape->DrawShape(pShapeDisObj);
			pHmodel->DisplayObj(pShapeDisObj);
		}
	}

}

void CMoveShapesCmd::AddCmdAndSetNewInfo()
{
	MY_ONLYADDCMDANDNOCALLCMD(this);
}
